﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraNavBar;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraSplashScreen;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraBars.Ribbon.Gallery;
using DevExpress.XtraRichEdit;
using DevExpress.XtraGrid;
using DevExpress.XtraPrinting;
using DevExpress.Utils.About;
using DevExpress.MailClient.Win.Data;

namespace DevExpress.MailClient.Win
{
    public partial class frmMain : PRO_DOCS.BLL.WIN.Templates.DevxPermissionRibbonForm
    {
        Dictionary<ApplicationModules, Control> APPLICATION_MODULES_CASHES
        {
            get;
            set;
        }

        public ApplicationModules _currentApplicationModule;
        public ApplicationModules CurrentApplicationModule
        {
            get { return _currentApplicationModule; }
            set
            {
                if (pcMain.Controls.Count > 0)
                {

                    APPLICATION_MODULES_CASHES[_currentApplicationModule] = pcMain.Controls[0];
                }
                pcMain.Controls.Clear();


                _currentApplicationModule = value;

                pcMain.Controls.Add(APPLICATION_MODULES_CASHES[_currentApplicationModule]);
                pcMain.Controls[0].Dock = DockStyle.Fill;
            }
        }



        public MyLockedOptions CURRENT_SELECTED_OPTION
        { get; set; }

        public enum MyLockedOptions
        {
            AllMyLockedFiles = 0,
            SpeceficDocumentType,
            SpeceficDate,
            BetweenDates
        }

        public enum ApplicationModules
        {
            FolderStructure,
            Mail,
            Calendar,
            Contacts,
            Feeds,
            TasksManager,
            MyRequests,
            Search,
            LockedFiles,
            MapsCategories,
            MapView
        }
        MailTypes currentMailType = MailTypes.Inbox;
        ModulesNavigator modulesNavigator;
        internal FilterColumnsManager FilterColumnManager;
        ZoomManager zoomManager;
        List<BarItem> AllowCustomizationMenuList = new List<BarItem>();
        public frmMain()
        {
            InitializeComponent();
            ucFolderStructureTree1.CURRENT_SELECTED_VIEW_MODE = ucFolderStructureTree.FolderStructureViewModes.MainForm;
            ucFolderStructureTree1.BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE();
            rpcSearch.Text = TagResources.SearchTools;
            InitNavBarGroups();
            SkinHelper.InitSkinGallery(rgbiSkins);
            RibbonButtonsInitialize();
            modulesNavigator = new ModulesNavigator(ribbonControl1, pcMain);
            modulesNavigator.ChangeGroup(nbcInternalMessaging.ActiveGroup, this);
            NavigationInitialize();

            //CurrentApplicationModule = ApplicationModules.FolderStructure;


            APPLICATION_MODULES_CASHES = new Dictionary<ApplicationModules, Control>();


            APPLICATION_MODULES_CASHES.Add(ApplicationModules.FolderStructure, new DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure());
            APPLICATION_MODULES_CASHES.Add(ApplicationModules.Mail, new DevExpress.MailClient.Win.Mail());
            APPLICATION_MODULES_CASHES.Add(ApplicationModules.LockedFiles, new DevExpress.MailClient.Win.Modules.MyLockedFiles());
            APPLICATION_MODULES_CASHES.Add(ApplicationModules.Search, new DevExpress.MailClient.Win.Modules.SearchingFolderStructure());
            APPLICATION_MODULES_CASHES.Add(ApplicationModules.MapsCategories, new DevExpress.MailClient.Win.Controls.eMap.ucRegionsManagement());
            APPLICATION_MODULES_CASHES.Add(ApplicationModules.MapView, new DevExpress.MailClient.Win.Modules.RegionMap());
            APPLICATION_MODULES_CASHES.Add(ApplicationModules.TasksManager, new DevExpress.MailClient.Win.TasksManager.TasksManagerViewer(TasksManager.TasksManagerViewer.WORKFLOW_WINDOW_TYPES.TASK_MANAGER));
            APPLICATION_MODULES_CASHES.Add(ApplicationModules.MyRequests, new DevExpress.MailClient.Win.TasksManager.TasksManagerViewer(TasksManager.TasksManagerViewer.WORKFLOW_WINDOW_TYPES.MY_REQUESTS));

            ucMailTree1 = new ucMailTree();
            navBarGroupControlContainer1.Controls.Add(ucMailTree1);

            CurrentApplicationModule = ApplicationModules.FolderStructure;

        }
        void NavigationInitialize()
        {
            foreach (NavBarGroup group in nbcInternalMessaging.Groups)
            {
                if (group.Visible)
                {
                    BarButtonItem item = new BarButtonItem(ribbonControl1.Manager, group.Caption);
                    item.Tag = group;
                    item.Glyph = group.SmallImage;
                    item.ItemClick += new ItemClickEventHandler(item_ItemClick);
                    bsiNavigation.ItemLinks.Add(item);
                }
            }
        }

        void item_ItemClick(object sender, ItemClickEventArgs e)
        {
            nbcInternalMessaging.ActiveGroup = (NavBarGroup)e.Item.Tag;
        }
        void RibbonButtonsInitialize()
        {
            InitBarButtonItem(bbiRotateLayout, TagResources.RotateLayout, Properties.Resources.RotateLayoutDescription);
            InitBarButtonItem(bbiFlipLayout, TagResources.FlipLayout, Properties.Resources.FlipLayoutDescription);
            //InitBarButtonItem(bbiDelete, TagResources.DeleteItem, Properties.Resources.DeleteItemDescription);
            InitBarButtonItem(bbiDeleteMail, TagResources.DeleteItem, Properties.Resources.DeleteItemDescription);
            InitBarButtonItem(bbiNew, TagResources.NewMail, Properties.Resources.NewItemDescription);
            InitBarButtonItem(bbiReply, TagResources.Reply, Properties.Resources.ReplyDescription);
            InitBarButtonItem(bbiReplyAll, TagResources.ReplyAll, Properties.Resources.ReplyAllDescription);
            InitBarButtonItem(bbiForward, TagResources.Forward, Properties.Resources.ForwardDescription);
            InitBarButtonItem(bbiUnreadRead, TagResources.UnreadRead, Properties.Resources.UnreadReadDescription);
            InitBarButtonItem(bbiCloseSearch, TagResources.CloseSearch, Properties.Resources.CloseSearchDescription);
            InitBarButtonItem(bbiSubjectColumn, TagResources.SubjectColumn);
            InitBarButtonItem(bbiFromColumn, TagResources.PersonColumn);
            InitBarButtonItem(bbiDateColumn, TagResources.DateColumn);
            InitBarButtonItem(bbiPriorityColumn, TagResources.PriorityColumn);
            InitBarButtonItem(bbiAttachmentColumn, TagResources.AttachmentColumn);
            InitBarButtonItem(bbiResetColumnsToDefault, TagResources.ResetColumnsToDefault);
            InitBarButtonItem(bbiDate, TagResources.DateFilterMenu);
            InitBarButtonItem(bbiClearFilter, TagResources.ClearFilter);
            InitBarButtonItem(bbiNewFeed, TagResources.FeedNew, Properties.Resources.NewFeedDescription);
            InitBarButtonItem(bbiEditFeed, TagResources.FeedEdit, Properties.Resources.EditFeedDescription);
            InitBarButtonItem(bbiDeleteFeed, TagResources.FeedDelete, Properties.Resources.DeleteFeedDescription);
            InitBarButtonItem(bbiRefreshFeed, TagResources.FeedRefresh, Properties.Resources.RefreshFeedDescription);
            InitBarButtonItem(bbiNewContact, TagResources.ContactNew, Properties.Resources.NewContactDescription);
            InitBarButtonItem(bbiEditContact, TagResources.ContactEdit, Properties.Resources.EditContactDescription);
            InitBarButtonItem(bbiDeleteContact, TagResources.ContactDelete, Properties.Resources.DeleteContactDescription);
            InitBarButtonItem(bbiNewTask, TagResources.TaskNew, Properties.Resources.NewTaskDescription);
            InitBarButtonItem(bbiEditTask, TagResources.TaskEdit, Properties.Resources.EditTaskDescription);
            InitBarButtonItem(bbiDeleteTask, TagResources.TaskDelete, Properties.Resources.DeleteTaskDescription);
            InitBarButtonItem(bbiTodayFlag, FlagStatus.Today, Properties.Resources.FlagTodayDescription);
            InitBarButtonItem(bbiTomorrowFlag, FlagStatus.Tomorrow, Properties.Resources.FlagTomorrowDescription);
            InitBarButtonItem(bbiThisWeekFlag, FlagStatus.ThisWeek, Properties.Resources.FlagThisWeekDescription);
            InitBarButtonItem(bbiNextWeekFlag, FlagStatus.NextWeek, Properties.Resources.FlagNextWeekDescription);
            InitBarButtonItem(bbiNoDateFlag, FlagStatus.NoDate, Properties.Resources.FlagNoDatekDescription);
            InitBarButtonItem(bbiCustomFlag, FlagStatus.Custom, Properties.Resources.FlagCustomDescription);
            InitBarButtonItem(bbiShowPreview, TagResources.Preview, Properties.Resources.ShowPreviewDescription);
            InitGalleryItem(rgbiCurrentView.Gallery.Groups[0].Items[0], TagResources.ContactList, Properties.Resources.ContactListDescription);
            InitGalleryItem(rgbiCurrentView.Gallery.Groups[0].Items[1], TagResources.ContactAlphabetical, Properties.Resources.ContactAlphabeticalDescription);
            InitGalleryItem(rgbiCurrentView.Gallery.Groups[0].Items[2], TagResources.ContactByState, Properties.Resources.ContactByStateDescription);
            InitGalleryItem(rgbiCurrentView.Gallery.Groups[0].Items[3], TagResources.ContactCard, Properties.Resources.ContactCardDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[0], TagResources.TaskList, Properties.Resources.TaskListDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[1], TagResources.TaskToDoList, Properties.Resources.TaskToDoListDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[2], TagResources.TaskCompleted, Properties.Resources.TaskCompletedDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[3], TagResources.TaskToday, Properties.Resources.TaskTodayDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[4], TagResources.TaskPrioritized, Properties.Resources.TaskPrioritizedDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[5], TagResources.TaskOverdue, Properties.Resources.TaskOverdueDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[6], TagResources.TaskSimpleList, Properties.Resources.TaskSimpleListDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[7], TagResources.TaskDeferred, Properties.Resources.TaskDeferredDescription);
            //bvbiSaveAs.Tag = TagResources.MenuSaveAs;
            //bvbiSaveAttachment.Tag = TagResources.MenuSaveAttachment;
            //bvbiSaveCalendar.Tag = TagResources.MenuSaveCalendar;
            bbiPriority.Hint = Properties.Resources.PriorityDescription;
            bsiNavigation.Hint = Properties.Resources.NavigationDescription;
            bbiShowUnread.Hint = Properties.Resources.SearchUnreadDescription;
            bbiDate.Hint = Properties.Resources.SearchReceivedDescription;
            bbiImportant.Hint = Properties.Resources.SearchImportantDescription;
            bbiHasAttachment.Hint = Properties.Resources.SearchHasAttachmentDescription;
            bbiClearFilter.Hint = Properties.Resources.SearchClearDescription;
            bsiColumns.Hint = Properties.Resources.SearchColumnsDescription;
            bbiResetColumnsToDefault.Hint = Properties.Resources.SearchResetDescription;
            bbiCloseSearch.Hint = Properties.Resources.SearchCloseDescription;

            List<BarButtonItem> items = new List<BarButtonItem>();
            items.Add(bbiSubjectColumn);
            items.Add(bbiFromColumn);
            items.Add(bbiDateColumn);
            items.Add(bbiPriorityColumn);
            items.Add(bbiAttachmentColumn);
            items.Add(bbiDate);
            FilterColumnManager = new FilterColumnsManager(items);
            ucContacts1.SynchronizeGalleryItems(rgbiCurrentView);
            ucCalendar1.SetBarController(schedulerBarController1);
            AllowCustomizationMenuList.Add(bbiFlipLayout);
            AllowCustomizationMenuList.Add(bbiRotateLayout);
            AllowCustomizationMenuList.Add(bsiNavigation);
            AllowCustomizationMenuList.Add(rgbiSkins);
            ribbonControl1.Toolbar.ItemLinks.Add(rgbiSkins);
        }

        void InitGalleryItem(GalleryItem galleryItem, string tag, string description)
        {
            galleryItem.Tag = tag;
            galleryItem.Hint = description;
        }
        internal bool ShowPreview { get { return bbiShowPreview.Down; } }
        internal ZoomManager ZoomManager { get { return zoomManager; } }
        internal BarButtonItem ShowUnreadItem { get { return bbiShowUnread; } }
        internal BarButtonItem ImportantItem { get { return bbiImportant; } }
        internal BarButtonItem HasAttachmentItem { get { return bbiHasAttachment; } }
        internal BarButtonItem ClearFilterItem { get { return bbiClearFilter; } }
        //internal BackstageViewButtonItem SaveAsMenuItem { get { return bvbiSaveAs; } }
        //internal BackstageViewButtonItem SaveAttachmentMenuItem { get { return bvbiSaveAttachment; } }
        //internal BackstageViewButtonItem SaveCalendar { get { return bvbiSaveCalendar; } }
        internal InRibbonGallery TaskGallery { get { return rgbiCurrentViewTasks.Gallery; } }
        internal PopupMenu FlagStatusMenu { get { return pmFlagStatus; } }
        void InitBarButtonItem(DevExpress.XtraBars.BarButtonItem buttonItem, object tag)
        {
            InitBarButtonItem(buttonItem, tag, string.Empty);
        }
        void InitBarButtonItem(DevExpress.XtraBars.BarButtonItem buttonItem, object tag, string description)
        {
            buttonItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(bbi_ItemClick);
            buttonItem.Hint = description;
            buttonItem.Tag = tag;
        }
        void InitNavBarGroups()
        {
            nbgDepartmentsFolderStructure.Tag = new NavBarGroupTagObject("FolderStructure", typeof(DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure));
            nbgMail.Tag = new NavBarGroupTagObject("Mail", typeof(DevExpress.MailClient.Win.Mail));
            nbgCalendar.Tag = new NavBarGroupTagObject("Calendar", typeof(DevExpress.MailClient.Win.Calendar));
            nbgContacts.Tag = new NavBarGroupTagObject("Contacts", typeof(DevExpress.MailClient.Win.Contacts));
            nbgFeeds.Tag = new NavBarGroupTagObject("Feeds", typeof(DevExpress.MailClient.Win.Feeds));
            nbgTasks.Tag = new NavBarGroupTagObject("Tasks", typeof(DevExpress.MailClient.Win.Tasks));
            nbgSearch.Tag = new NavBarGroupTagObject("Search", typeof(DevExpress.MailClient.Win.Modules.SearchingFolderStructure));
            nbgMyLockedFiles.Tag = new NavBarGroupTagObject("LockedFiles", typeof(DevExpress.MailClient.Win.Modules.MyLockedFiles));
            nbgMaps.Tag = new NavBarGroupTagObject("MapsCategories", typeof(DevExpress.MailClient.Win.Controls.eMap.ucRegionsManagement));
        }
        public void ReadMessagesChanged()
        {
            ucMailTree1.RefreshTreeList();
        }
        public void UpdateTreeViewMessages()
        {
           ucMailTree1.UpdateTreeViewMessages();
        }
        internal void EnableDelete(bool enabled)
        {
            //bbiDelete.Enabled = enabled;
            bbiDeleteMail.Enabled = enabled;
            bbiUnreadRead.Enabled = enabled;
            bbiPriority.Enabled = enabled;
        }
        internal void EnableMail(bool enabled, bool unread)
        {
            bbiReply.Enabled = enabled && currentMailType == MailTypes.Inbox;
            bbiReplyAll.Enabled = enabled && currentMailType == MailTypes.Inbox;
            bbiForward.Enabled = enabled && currentMailType == MailTypes.Inbox;
        }
        internal void EnableEditFeed(bool enabled)
        {
            bbiDeleteFeed.Enabled = enabled;
            bbiEditFeed.Enabled = enabled;
            bbiRefreshFeed.Enabled = enabled;
        }
        internal void EnableEditContact(bool enabled)
        {
            bbiDeleteContact.Enabled = enabled;
            bbiEditContact.Enabled = enabled;
        }
        internal void EnableLayoutButtons(bool enabled)
        {
            bbiRotateLayout.Enabled = enabled;
            bbiFlipLayout.Enabled = enabled;
        }
        internal void EnabledFlagButtons(bool enabled, FlagStatus status)
        {
            List<BarButtonItem> list = new List<BarButtonItem> { bbiTodayFlag, bbiTomorrowFlag, bbiThisWeekFlag, 
                bbiNextWeekFlag, bbiNoDateFlag, bbiCustomFlag };
            foreach (BarButtonItem item in list)
            {
                item.Enabled = enabled;
                item.Down = status.Equals(item.Tag);
            }
            bbiDeleteTask.Enabled = enabled;
            bbiEditTask.Enabled = enabled;
        }

        internal void SetPriorityMenu(PopupMenu menu)
        {
            bbiPriority.DropDownControl = menu;
        }
        internal void SetDateFilterMenu(PopupMenu menu)
        {
            bbiDate.DropDownControl = menu;
        }
        internal void ShowMessageMenu(Point location)
        {
            pmMessage.ShowPopup(location);
        }
        internal void ShowInfo(int? count)
        {
            if (count == null) bsiInfo.Caption = string.Empty;
            else
                bsiInfo.Caption = string.Format(Properties.Resources.InfoText, count.Value);
            HtmlText = string.Format("{0}{1}", GetModuleName(), GetModulePartName());
        }
        string GetModuleName()
        {
            if (string.IsNullOrEmpty(modulesNavigator.CurrentModule.PartName)) return CurrentModuleName;
            return string.Format("<b>{0}</b>", CurrentModuleName);
        }
        string GetModulePartName()
        {
            if (string.IsNullOrEmpty(modulesNavigator.CurrentModule.PartName)) return null;
            return string.Format(" - {0}", modulesNavigator.CurrentModule.PartName);
        }
        private void navBarControl1_ActiveGroupChanged(object sender, DevExpress.XtraNavBar.NavBarGroupEventArgs e)
        {
            object data = GetModuleData((NavBarGroupTagObject)e.Group.Tag);
            modulesNavigator.ChangeGroup(e.Group, data);
            // CurrentApplicationModule = (ApplicationModules)Enum.Parse(typeof(ApplicationModules),CurrentModuleName);
        }
        private void bbi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }
        private void ucMailTree1_DataSourceChanged(object sender, DataSourceChangedEventArgs e)
        {
            currentMailType = e.Type;
            modulesNavigator.CurrentModule.MessagesDataChanged(e);
            ShowInfo(e.List.Count);
        }
        private void ucMailTree1_ShowMenu(object sender, MouseEventArgs e)
        {
            pmTreeView.ShowPopup(ucMailTree1.PointToScreen(e.Location));
        }
        private void pmTreeView_BeforePopup(object sender, CancelEventArgs e)
        {
            bciShowAllMessageCount.Checked = DataHelper.ShowAllMessageCount;
            bciShowUnreadMessageCount.Checked = DataHelper.ShowUnreadMessageCount;
        }
        private void bciShowAllMessageCount_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataHelper.ShowAllMessageCount = bciShowAllMessageCount.Checked;
            ucMailTree1.RefreshTreeList();
        }
        private void bciShowUnreadMessageCount_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataHelper.ShowUnreadMessageCount = bciShowUnreadMessageCount.Checked;
            ucMailTree1.RefreshTreeList();
        }
        //private void frmMain_KeyDown(object sender, KeyEventArgs e)
        //{
        //    modulesNavigator.CurrentModule.SendKeyDown(e);
        //}
        protected object GetModuleData(NavBarGroupTagObject tag)
        {
            if (tag == null) return null;
            if (tag.ModuleType == typeof(DevExpress.MailClient.Win.Calendar)) return ucCalendar1;
            if (tag.ModuleType == typeof(DevExpress.MailClient.Win.Feeds)) return navBarControl2;
            if (tag.ModuleType == typeof(DevExpress.MailClient.Win.Tasks)) return nbgTasks;
            return null;
        }
        private void navBarControl1_NavPaneStateChanged(object sender, EventArgs e)
        {
            ucCalendar1.StateChanged(nbcInternalMessaging.OptionsNavPane.ActualNavPaneState);
        }
        private void ucContacts1_CheckedChanged(object sender, EventArgs e)
        {
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", ((CheckEdit)sender).Tag));
        }

        private void bvbiExit_ItemClick(object sender, BackstageViewItemEventArgs e)
        {
            this.Close();
        }

        private void galleryControlGallery1_ItemClick(object sender, GalleryItemClickEventArgs e)
        {
            if (TagResources.OpenCalendar.Equals(e.Item.Tag))
            {
                ribbonControl1.HideApplicationButtonContentControl();
                this.Refresh();
                nbcInternalMessaging.ActiveGroup = nbgCalendar;
            }
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }

        private void backstageViewControl1_ItemClick(object sender, BackstageViewItemEventArgs e)
        {
            if (modulesNavigator.CurrentModule == null) return;
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }

        private void rgbiCurrentView_GalleryInitDropDownGallery(object sender, InplaceGalleryEventArgs e)
        {
            e.PopupGallery.GalleryDropDown.ItemLinks.Add(bbiManageView);
            e.PopupGallery.GalleryDropDown.ItemLinks.Add(bbiSaveCurrentView);
            e.PopupGallery.SynchWithInRibbonGallery = true;
        }

        private void rgbiCurrentViewTasks_GalleryItemClick(object sender, GalleryItemClickEventArgs e)
        {
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }

        private void ucCalendar1_VisibleChanged(object sender, EventArgs e)
        {
            if (ucCalendar1.Visible)
                ucCalendar1.UpdateTreeListHeight();
        }

        private void bvtiPrint_SelectedChanged(object sender, BackstageViewItemEventArgs e)
        {
            //if (backstageViewControl1.SelectedTab == bvtiPrint)
            //    this.printControl1.InitPrintingSystem();
        }
        private void ribbonControl1_BeforeApplicationButtonContentControlShow(object sender, EventArgs e)
        {
            //if(backstageViewControl1.SelectedTab == bvtiPrint) backstageViewControl1.SelectedTab = bvtiInfo;
            //bvtiPrint.Enabled = CurrentRichEdit != null || CurrentPrintableComponent != null;
            //bvtiExport.Enabled = CurrentExportComponent != null;
        }
        public IPrintable CurrentPrintableComponent { get { return modulesNavigator.CurrentModule.PrintableComponent; } }
        public IPrintable CurrentExportComponent { get { return modulesNavigator.CurrentModule.ExportComponent; } }
        public RichEditControl CurrentRichEdit { get { return modulesNavigator.CurrentModule.CurrentRichEdit; } }
        public string CurrentModuleName { get { return modulesNavigator.CurrentModule.ModuleName; } }

        private void ribbonControl1_ShowCustomizationMenu(object sender, RibbonCustomizationMenuEventArgs e)
        {
            e.CustomizationMenu.InitializeMenu();
            if (e.Link == null || !AllowCustomizationMenuList.Contains(e.Link.Item))
                e.CustomizationMenu.RemoveLink(e.CustomizationMenu.ItemLinks[0]);
        }

        internal void SET_FILES_BUTTONS_ENABABLING(PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE aRCHIVE_DEPARTMENT_FILE, bool filePhysicallyfound)
        {
            bbiDeleteFile.Enabled = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty);
            bbiSaveAsCopy.Enabled = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty);
            bbiCopyDocument.Enabled = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty);
            bbiMoveFile.Enabled = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty);
            bbiSaveDocumentData.Enabled = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty);
            bbiDownloadFile.Enabled = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty) && filePhysicallyfound;
            bbiPrintBarcode.Enabled = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty);
            bbiStartWorkFlow.Enabled = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty);
            bbiSendUsingEmail.Enabled = !aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty);
            #region GET LOCKED STATUS
            if (!aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty))
            {
                PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENT_FILE_LOCKED_STATUSLoadAllUNLOCKEDByFILE_IDResult CURRENT_LOCKED_FILE =
                        PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILE_LOCKED_STATUSLoadAllUNLOCKEDByFILE_ID(aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID).FirstOrDefault();

                //if it is previously locked
                if (CURRENT_LOCKED_FILE != null)
                {
                    if (CURRENT_LOCKED_FILE.ARCHIVE_DEPARTMENT_FILE_IS_LOCKED_BY_USER_ID.Value.Equals(
                        PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID))
                    {
                        bbiUnlockFile.Enabled = true;

                        bbiLockFile.Enabled = false;

                        bbiSaveDocumentData.Enabled = true;
                        bbiDeleteFile.Enabled = true;
                    }
                    else
                    {

                        bbiUnlockFile.Enabled = false;

                        bbiLockFile.Enabled = false;


                        bbiSaveDocumentData.Enabled = false;
                        bbiDeleteFile.Enabled = false;
                    }
                }
                else
                {
                    bbiLockFile.Enabled = true;
                    bbiUnlockFile.Enabled = false;
                }

            }
            else
            {
                bbiLockFile.Enabled = false;
                bbiUnlockFile.Enabled = false;

            }
            #endregion

        }


        private void FILES_BUTTONS_ItemClick(object sender, ItemClickEventArgs e)
        {
            string sELECTED_BUTTON_TAG = e.Item.Tag.ToString();

            switch (sELECTED_BUTTON_TAG)
            {
                #region FILES EDITING
                case "copyFile":
                    {
                        Modules.DepartmentsFolderStructure departmentsFolderStructure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                        departmentsFolderStructure.COPY_CURRENT_SELECTED_FILE();
                    }
                    break;
                case "saveFileAs":
                    {
                        Modules.DepartmentsFolderStructure departmentsFolderStructure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                        departmentsFolderStructure.SAVE_AS_CURRENT_SELECTED_FILE();
                    }
                    break;
                case "moveFile":
                    {
                        Modules.DepartmentsFolderStructure departmentsFolderStructure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                        departmentsFolderStructure.MOVE_CURRENT_SELECTED_FILE();
                    }
                    break;
                #endregion

                #region Add Files
                case "AddFiles":
                    {
                        PRO_DOCS.DocumentManagementSystem.CLIENT.WIN.Forms.frmDepartmentFilesBulkInsert departmentFilesBulkInsert = new PRO_DOCS.DocumentManagementSystem.CLIENT.WIN.Forms.frmDepartmentFilesBulkInsert();
                        if (ucFolderStructureTree1.CURRENT_SELECTED_ApplicationStructureType.Equals(ApplicationStructureTypes.Folder))
                        {
                            departmentFilesBulkInsert.CURRENT_SELECTED_DESTINATION_ARCHIVE_DEPARTMENTS_FOLDER = ucFolderStructureTree1.CURRENT_SELECTED_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE;
                        }
                        departmentFilesBulkInsert.ShowDialog();

                        if (departmentFilesBulkInsert.FILES_ADDED)
                        {
                            Modules.DepartmentsFolderStructure current_department_folder_structure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                            current_department_folder_structure.RELOAD_ARCHIVE_DEPARTMENT_FILES();
                        }

                        departmentFilesBulkInsert = null;
                    }
                    break;
                #endregion

                #region Refresh Files
                case "RefreshFiles":
                    {
                        Modules.DepartmentsFolderStructure current_department_folder_structure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                        current_department_folder_structure.RELOAD_ARCHIVE_DEPARTMENT_FILES();
                    }
                    break;
                #endregion

                #region Print barcode
                case "printBarcode":
                    {
                        Modules.DepartmentsFolderStructure current_department_folder_structure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                        current_department_folder_structure.PRINT_FILE_BARCODE();
                    }
                    break;
                #endregion

                #region Routing
                case "StartRouting":
                    {
                        switch (CurrentApplicationModule)
                        {
                            case ApplicationModules.FolderStructure:
                                {
                                    Modules.DepartmentsFolderStructure current_department_folder_structure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                                    if (current_department_folder_structure.START_FILE_WORKFLOW())
                                    {
                                        bbiLockFile.Enabled = false;
                                        bbiUnlockFile.Enabled = false;
                                    }

                                } break;
                        }
                    }
                    break;
                #endregion

                #region Delete File
                case "DeleteFile":
                    {
                        switch (CurrentApplicationModule)
                        {
                            case ApplicationModules.FolderStructure:
                                {
                                    if (XtraMessageBox.Show(PRO_DOCS.BLL.WIN.Properties.Messages.Default.ConfirmDeleteText.Replace("\"{name}\"", "the selected file"), PRO_DOCS.BLL.WIN.Properties.Messages.Default.DeleteCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Error).Equals(DialogResult.OK))
                                    {
                                        Modules.DepartmentsFolderStructure current_department_folder_structure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                                        current_department_folder_structure.DELETE_CURRENT_SELECTED_FILE();
                                    }
                                }
                                break;
                        }

                    }
                    break;
                #endregion

                #region Save Update
                case "SaveUpdate":
                    {
                        switch (CurrentApplicationModule)
                        {
                            case ApplicationModules.FolderStructure:
                                {
                                    Modules.DepartmentsFolderStructure current_department_folder_structure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                                    current_department_folder_structure.SAVE_SELECTED_FILE_DYNAMIC_INDICIES();
                                }
                                break;
                            case ApplicationModules.Mail:
                                break;
                            case ApplicationModules.Calendar:
                                break;
                            case ApplicationModules.Contacts:
                                break;
                            case ApplicationModules.Feeds:
                                break;
                            case ApplicationModules.TasksManager:
                                break;
                            case ApplicationModules.Search:
                                break;
                            case ApplicationModules.LockedFiles:
                                {
                                    Modules.MyLockedFiles current_MyLockedFiles = (DevExpress.MailClient.Win.Modules.MyLockedFiles)pcMain.Controls[0];
                                    current_MyLockedFiles.SAVE_SELECTED_FILE_DYNAMIC_INDICIES();
                                }
                                break;
                        }
                    }
                    break;
                #endregion

                #region Scan Files
                case "ScanFiles":
                    {
                        Modules.DepartmentsFolderStructure current_department_folder_structure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                        current_department_folder_structure.START_SCANNING();
                    }
                    break;
                #endregion

                #region Download File
                case "DownloadFile":
                    {
                        switch (CurrentApplicationModule)
                        {
                            case ApplicationModules.FolderStructure:
                                {
                                    Modules.DepartmentsFolderStructure current_department_folder_structure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                                    current_department_folder_structure.DOWNLOAD_SELECTED_FILE();
                                } break;
                            case ApplicationModules.Mail:
                                break;
                            case ApplicationModules.Calendar:
                                break;
                            case ApplicationModules.Contacts:
                                break;
                            case ApplicationModules.Feeds:
                                break;
                            case ApplicationModules.TasksManager:
                                break;
                            case ApplicationModules.Search:
                                break;
                            case ApplicationModules.LockedFiles:
                                {
                                    Modules.MyLockedFiles current_MyLockedFiles = (DevExpress.MailClient.Win.Modules.MyLockedFiles)pcMain.Controls[0];
                                    current_MyLockedFiles.DOWNLOAD_SELECTED_FILE();
                                }
                                break;
                        }
                    }
                    break;
                #endregion

                #region LOCK and Unlock
                case "LockFile":
                    {

                        Modules.DepartmentsFolderStructure current_department_folder_structure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                        current_department_folder_structure.LOCK_CURRENT_FILE();
                        bbiLockFile.Enabled = false;
                        bbiUnlockFile.Enabled = true;
                    }
                    break;
                case "UnlockFile":
                    {
                        switch (CurrentApplicationModule)
                        {
                            case ApplicationModules.FolderStructure:
                                {
                                    Modules.DepartmentsFolderStructure current_department_folder_structure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                                    current_department_folder_structure.UNLOCK_CURRENT_FILE();
                                    bbiLockFile.Enabled = true;
                                    bbiUnlockFile.Enabled = false;
                                } break;
                            case ApplicationModules.Mail:
                                break;
                            case ApplicationModules.Calendar:
                                break;
                            case ApplicationModules.Contacts:
                                break;
                            case ApplicationModules.Feeds:
                                break;
                            case ApplicationModules.TasksManager:
                                break;
                            case ApplicationModules.Search:
                                break;
                            case ApplicationModules.LockedFiles:
                                {
                                    Modules.MyLockedFiles current_MyLockedFiles = (DevExpress.MailClient.Win.Modules.MyLockedFiles)pcMain.Controls[0];
                                    current_MyLockedFiles.UNLOCK_CURRENT_FILE();
                                    bbiLockFile.Enabled = true;
                                    bbiUnlockFile.Enabled = false;
                                }
                                break;
                        }

                    }
                    break;
                #endregion

                #region Locked Files
                case "ReloadLockedFiles":
                    {
                        GET_MY_LOCKED_FILES(MY_SELECTED_LOCKED_FILES_OPTION, MY_SELECTED_DOCUMENT_TYPES, MY_SELECTED_FROM_DATE, MY_SELECTED_TO_DATE);
                    }
                    break;
                #endregion

                #region SEARCH FILES
                case "SearchFiles":
                    {
                        GET_FILES_SEARCH_RESULTS();
                    }
                    break;
                #endregion

                #region MAPS VIEW
                case "AddMapRegion":
                    {
                        DevExpress.MailClient.Win.Controls.eMap.ucRegionsManagement ucRegionsManagement = (Controls.eMap.ucRegionsManagement)pcMain.Controls[0];
                        ucRegionsManagement.ADD_NEW_MAP_REGION();
                    }
                    break;

                case "SaveMapRegion":
                    {
                        DevExpress.MailClient.Win.Controls.eMap.ucRegionsManagement ucRegionsManagement = (Controls.eMap.ucRegionsManagement)pcMain.Controls[0];
                        ucRegionsManagement.SAVE_UPDATE_MAP_REGION();
                    }
                    break;

                case "DeleteMapRegion":
                    {
                        DevExpress.MailClient.Win.Controls.eMap.ucRegionsManagement ucRegionsManagement = (Controls.eMap.ucRegionsManagement)pcMain.Controls[0];
                        ucRegionsManagement.DELETE_MAP_REGION();
                    }
                    break;
                #endregion

                #region TASKS
                case "takeAction":
                    {
                        TasksManager.TasksManagerViewer taskManagerViewer = (TasksManager.TasksManagerViewer)pcMain.Controls[0];
                        taskManagerViewer.SET_STEP_ACTION(TasksManager.TasksManagerViewer.WORKFLOW_ACTIONS.N_A);
                    }
                    break;
                case "approveWorkFlow":
                    {
                        TasksManager.TasksManagerViewer taskManagerViewer = (TasksManager.TasksManagerViewer)pcMain.Controls[0];
                        taskManagerViewer.SET_STEP_ACTION(TasksManager.TasksManagerViewer.WORKFLOW_ACTIONS.APPROVE);
                    }
                    break;
                case "rejectWorkflow":
                    {
                        TasksManager.TasksManagerViewer taskManagerViewer = (TasksManager.TasksManagerViewer)pcMain.Controls[0];
                        taskManagerViewer.SET_STEP_ACTION(TasksManager.TasksManagerViewer.WORKFLOW_ACTIONS.REJECT);
                    }
                    break;
                #endregion

                #region MAILS
                case "SendUsingEmail":
                    {
                        Modules.DepartmentsFolderStructure departmentsFolderStructure = (DevExpress.MailClient.Win.Modules.DepartmentsFolderStructure)pcMain.Controls[0];
                        departmentsFolderStructure.SEND_SELECTED_FILE();
                    }
                    break;
                case "SendAndReceive":
                    {
                        DevExpress.MailClient.Win.Mail mAIL = (DevExpress.MailClient.Win.Mail)pcMain.Controls[0];
                        mAIL.SEND_AND_RECEIVE();
                    }
                    break;
                case "DeleteMail":
                    {
                        DevExpress.MailClient.Win.Mail mAIL = (DevExpress.MailClient.Win.Mail)pcMain.Controls[0];
                        mAIL.DELETE_MAIL();
                    }
                    break;
                #endregion
            }
        }

        private void GET_FILES_SEARCH_RESULTS()
        {
            Modules.SearchingFolderStructure searchingFolderStructure = (Modules.SearchingFolderStructure)pcMain.Controls[0];
            searchingFolderStructure.GET_FILES_SEARCH_RESULTS();
        }

        private void nbgDepartmentsFolderStructure_ItemChanged(object sender, EventArgs e)
        {
            CurrentApplicationModule = ApplicationModules.FolderStructure;
        }

        private void nbgMyLockedFiles_ItemChanged(object sender, EventArgs e)
        {
            CurrentApplicationModule = ApplicationModules.LockedFiles;
        }

        private void nbgSearch_ItemChanged(object sender, EventArgs e)
        {
            CurrentApplicationModule = ApplicationModules.Search;
        }


        Modules.MyLockedFiles MY_LOCKED_FILE_FORM
        {
            get;
            set;
        }

        List<decimal> MY_SELECTED_DOCUMENT_TYPES { get; set; }

        DateTime MY_SELECTED_FROM_DATE
        { get; set; }

        DateTime MY_SELECTED_TO_DATE
        { get; set; }
        MyLockedOptions MY_SELECTED_LOCKED_FILES_OPTION
        { get; set; }
        internal void GET_MY_LOCKED_FILES(MyLockedOptions myLockedOptions, List<decimal> selectedocumentTypes, DateTime dtFromDate, DateTime dtToDate)
        {
            try
            {
                MY_SELECTED_LOCKED_FILES_OPTION = myLockedOptions;
                MY_SELECTED_DOCUMENT_TYPES = selectedocumentTypes;
                MY_SELECTED_FROM_DATE = dtFromDate;
                MY_SELECTED_TO_DATE = dtToDate;

                MY_LOCKED_FILE_FORM = (Modules.MyLockedFiles)pcMain.Controls[0];
                List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE> _aRCHIVE_DEPARTMENT_FILEs = new List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE>();

                List<PRO_DOCS.DAL.Database.GET_MY_LOCKED_FILESResult> gET_MY_LOCKED_FILESResults = new List<PRO_DOCS.DAL.Database.GET_MY_LOCKED_FILESResult>();
                switch (myLockedOptions)
                {
                    #region All My Locked Files
                    case MyLockedOptions.AllMyLockedFiles:
                        {
                            gET_MY_LOCKED_FILESResults = PRO_DOCSDatabaseDataContext.GET_MY_LOCKED_FILES
                                (
                                    Convert.ToInt32(myLockedOptions),
                                    PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID,
                                    string.Empty,
                                    DateTime.Now,
                                    DateTime.Now
                                ).ToList();

                        }
                        break;
                    #endregion
                    case MyLockedOptions.SpeceficDocumentType:
                        {
                            string document_types_ids = string.Empty;
                            for (int i = 0; i < selectedocumentTypes.Count; i++)
                            {
                                gET_MY_LOCKED_FILESResults.AddRange
                                    (
                                    PRO_DOCSDatabaseDataContext.GET_MY_LOCKED_FILES
                                                           (
                                                               Convert.ToInt32(myLockedOptions),
                                                               PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID,
                                                               selectedocumentTypes[i].ToString(),
                                                               DateTime.Now,
                                                               DateTime.Now
                                                           ).ToList()
                                    );
                                //document_types_ids += (i.Equals(0)) ? selectedocumentTypes[i].ToString() : "," + selectedocumentTypes[i].ToString();
                            }

                        }
                        break;
                    case MyLockedOptions.SpeceficDate:
                    case MyLockedOptions.BetweenDates:
                        {
                            DateTime toDate = dtToDate.AddDays(1).Date;
                            gET_MY_LOCKED_FILESResults = PRO_DOCSDatabaseDataContext.GET_MY_LOCKED_FILES
                                (
                                    Convert.ToInt32(myLockedOptions),
                                    PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID,
                                    string.Empty,
                                    dtFromDate.Date,
                                    toDate
                                ).ToList();
                        }
                        break;
                }

                for (int i = 0; i < gET_MY_LOCKED_FILESResults.Count; i++)
                {
                    _aRCHIVE_DEPARTMENT_FILEs.Add
                        (
                                (
                                    from files in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILEs
                                    where
                                    (
                                        files.ARCHIVE_DEPARTMENT_FILE_ID.Equals(gET_MY_LOCKED_FILESResults[i].ARCHIVE_DEPARTMENT_FILE_ID)
                                    )
                                    select files
                                ).FirstOrDefault()
                        );
                }
                MY_LOCKED_FILE_FORM.LOAD_ARCHIVE_DEPARTMENT_FILES(_aRCHIVE_DEPARTMENT_FILEs);
            }
            catch { }
        }

        private void nbgMail_ItemChanged(object sender, EventArgs e)
        {
            CurrentApplicationModule = ApplicationModules.Mail;
        }

        private void navBarGroup1_ItemChanged(object sender, EventArgs e)
        {
            CurrentApplicationModule = ApplicationModules.MapsCategories;
        }

        internal void GET_SEARCH_OPTION_VIEW(Win.Controls.ucSearchFiles.MySearchOptions CURRENT_SELECTED_SEARCH_OPTION)
        {
            try
            {
                Modules.SearchingFolderStructure searchingFolderStructure = (Modules.SearchingFolderStructure)pcMain.Controls[0];
                searchingFolderStructure.CURRENT_SELECTED_SEARCH_OPTION = CURRENT_SELECTED_SEARCH_OPTION;
            }
            catch { }
        }

        private void nbiWellsRegions_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            CurrentApplicationModule = ApplicationModules.MapsCategories;
            rpgMapRegions.Visible = true;
            rpgMapViewer.Visible = false;
        }

        private void nbiWellsMaps_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            CurrentApplicationModule = ApplicationModules.MapView;
            rpgMapRegions.Visible = false;
            rpgMapViewer.Visible = true;
        }

        private void nbiMyRequestedFiles_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            CurrentApplicationModule = ApplicationModules.MyRequests;

            TasksManager.TasksManagerViewer taskManagerViewer = (TasksManager.TasksManagerViewer)pcMain.Controls[0];
            taskManagerViewer.CURRENT_SELECTED_WORKFLOW_WINDOW_TYPE = TasksManager.TasksManagerViewer.WORKFLOW_WINDOW_TYPES.MY_REQUESTS;

            rpgWorkFlowDecesion.Visible = false;
        }

        private void nbiTaskManager_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            CurrentApplicationModule = ApplicationModules.TasksManager;

            TasksManager.TasksManagerViewer taskManagerViewer = (TasksManager.TasksManagerViewer)pcMain.Controls[0];
            taskManagerViewer.CURRENT_SELECTED_WORKFLOW_WINDOW_TYPE = TasksManager.TasksManagerViewer.WORKFLOW_WINDOW_TYPES.TASK_MANAGER;

            rpgWorkFlowDecesion.Visible = true;
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

    }
}
